/***************************************************************
 *  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
 *  All rights reserved
 *
 *  This file is part of the GroupMe! Project. You can utilize
 *  software of the GroupMe! project under the terms of the 
 *  GNU Lesser General Public License as published by the
 *  Free Software Foundation in version 2.1.
 * 
 *  The GNU Lesser General Public License can be found at:
 *  http://www.gnu.org/licenses/lgpl.html
 *
 *  This copyright notice MUST APPEAR in all copies of the file!
 ***************************************************************/
package net.groupme.model.dao;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

import javax.sql.DataSource;

import net.groupme.exceptions.DeleteFailedException;
import net.groupme.exceptions.GroupVisualizationException;
import net.groupme.exceptions.InsertFailedException;
import net.groupme.exceptions.UpdateFailedException;
import net.groupme.gui.constants.VIEW_VOCABULARY;
import net.groupme.model.GroupVisualization;
import net.groupme.model.MODEL_CONSTANTS;
import net.groupme.model.Resource;
import net.groupme.model.User;
import net.groupme.utils.URLResolver;
import net.groupme.utils.sql.GetGroupVisualizationsQuery;
import net.groupme.utils.sql.GetResourcesQuery;
import net.groupme.utils.sql.GetUsersQuery;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.SqlUpdate;

/**
 * 
 * The class/interface GroupDAOImpl ...
 * 
 * @author Mischa Frank <a
 *         href="mailto:code@planetfrank.net">code@planetfrank.net</a>
 * @author Fabian Abel, <a href="mailto:abel@l3s.de">abel@l3s.de</a>
 * @author last edited by: $Author: fabian $
 * 
 * @version $Revision: 1.19 $ $Date: 2009-04-22 18:28:55 $
 */
public class GroupDAOImpl extends ResourceDAOImpl implements GroupDAO {

	// table
	private static final String TABLE_GROUPRESOURCE = DBProperties
			.getString("GroupmeDB.TABLE_GROUPRESOURCE"); //$NON-NLS-1$

	// columns
	private static final String GROUPRESOURCE_GROUPID = DBProperties
			.getString("GroupmeDB.GROUPRESOURCE_GROUPID"); //$NON-NLS-1$

	private static final String GROUPRESOURCE_RESOURCEID = DBProperties
			.getString("GroupmeDB.GROUPRESOURCE_RESOURCEID"); //$NON-NLS-1$

	// table
	private static final String TABLE_USERGROUP = DBProperties
			.getString("GroupmeDB.TABLE_USERGROUP"); //$NON-NLS-1$

	// columns
	private static final String USERGROUP_USER_ID = DBProperties
			.getString("GroupmeDB.USERGROUP_USER_ID"); //$NON-NLS-1$

	private static final String USERGROUP_GROUP_ID = DBProperties
			.getString("GroupmeDB.USERGROUP_GROUP_ID"); //$NON-NLS-1$

	private static final String USERGROUP_ROLE = DBProperties
			.getString("GroupmeDB.USERGROUP_ROLE"); //$NON-NLS-1$

	// table
	private static final String TABLE_USERGROUPVISUALIZATION = DBProperties
			.getString("GroupmeDB.TABLE_USERGROUPVISUALIZATION"); //$NON-NLS-1$

	// columns
	private static final String USERGROUPVISUALIZATION_VISUALIZATION_ID = DBProperties
			.getString("GroupmeDB.USERGROUPVISUALIZATION_VISUALIZATION_ID"); //$NON-NLS-1$

	private static final String USERGROUPVISUALIZATION_USER_ID = DBProperties
			.getString("GroupmeDB.USERGROUPVISUALIZATION_USER_ID"); //$NON-NLS-1$

	private static final String USERGROUPVISUALIZATION_GROUP_ID = DBProperties
			.getString("GroupmeDB.USERGROUPVISUALIZATION_GROUP_ID"); //$NON-NLS-1$

	private static final String USERGROUPVISUALIZATION_ACTUAL = DBProperties
			.getString("GroupmeDB.USERGROUPVISUALIZATION_ACTUAL"); //$NON-NLS-1$

	private static final String USERGROUPVISUALIZATION_ROLE = DBProperties
		.getString("GroupmeDB.USERGROUPVISUALIZATION_ROLE"); //$NON-NLS-1$

	/** Logger for this class and subclasses */
	private static final Logger log = Logger.getLogger(GroupDAOImpl.class);

	private static final String DEFAULT_ROLE = MODEL_CONSTANTS.ROLE_OWNER;

	private DataSource ds;

	private String query;

	public GroupDAOImpl(DataSource ds, String id) {
		super(ds, id);
		this.ds = ds;
	}

	public GroupDAOImpl(DataSource ds, String name, String description)
			throws InsertFailedException{
		super(ds, name, description, MODEL_CONSTANTS.RESOURCE_TYPE_GROUP, null);
		this.ds = ds;
		try {
			//safe URL of Group in DataBase...
			this.setURL(this.getURL());
		} catch (UpdateFailedException e) {
			log.error("Storing of URL failed.", e);
		}
		log.info("Group Resource created. ID: " + this.id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Group#addResource(net.groupme.model.Resource)
	 */
	public void addResource(Resource resource) throws InsertFailedException {
		SqlUpdate su = new SqlUpdate(
				ds,
				"INSERT INTO " + TABLE_GROUPRESOURCE //$NON-NLS-1$
						+ " (" + GROUPRESOURCE_GROUPID + ", " + GROUPRESOURCE_RESOURCEID + ") VALUES (?, ?)"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 
		su.declareParameter(new SqlParameter("gID", Types.INTEGER)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("rID", Types.INTEGER)); //$NON-NLS-1$
		su.compile();
		try {
			Object[] oa = new Object[2];
			oa[0] = new Integer(this.id);
			oa[1] = new Integer(resource.getId());
			int count = su.update(oa);
			if (count == 0)
				throw new InsertFailedException();
		} catch (NumberFormatException e) {
			log.debug(e.getMessage());
		} 
		//Method for informing the logging table of the creation of the resource
		SqlUpdate su2 = new SqlUpdate(ds,
				"Insert Into " + DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION 
				+ " (" + DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_USER_ID + ", "
			    + DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_GROUP_ID + ", "
				+ DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_RESOURCE_ID + ", "
				+ DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_TAG_ID + ", "
				+ DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_ACTION  
				 + ") VALUES (?, ?, ?, ?, ?)");
		su2.declareParameter(new SqlParameter("UserID", Types.INTEGER)); 
		su2.declareParameter(new SqlParameter("GroupID", Types.INTEGER));
		su2.declareParameter(new SqlParameter("ResourceID", Types.INTEGER)); 
		su2.declareParameter(new SqlParameter("TagID", Types.INTEGER)); 		
		su2.declareParameter(new SqlParameter("Action", Types.VARCHAR)); 	
		Object[] oa2 = new Object[5];
		oa2[0] = ((User)(this.getUsers(DATABASE_CONSTANTS.User_ROLE_OWNER).toArray()[0])).getId();
		oa2[1] = this.id;
		oa2[2] = resource.getId();
		oa2[3] = null;
		oa2[4] = DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_RESOURCE_CREATED;
		int count2 = su2.update(oa2);
		if (count2 == 0){
			throw new InsertFailedException();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Group#addUser(java.lang.String,
	 *      net.groupme.model.User)
	 */
	public void addUser(String role, User user) throws InsertFailedException {
		SqlUpdate su = new SqlUpdate(
				ds,
				"INSERT INTO " + TABLE_USERGROUP //$NON-NLS-1$
						+ " (" + USERGROUP_USER_ID + ", " + USERGROUP_GROUP_ID + ", " + USERGROUP_ROLE + ") VALUES (?, ?, ?)"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		su.declareParameter(new SqlParameter("uID", Types.INTEGER)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("gID", Types.INTEGER)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("role", Types.VARCHAR)); //$NON-NLS-1$
		su.compile();
		try {
			Object[] oa = new Object[3];
			oa[0] = new Integer(user.getId());
			oa[1] = new Integer(this.id);
			oa[2] = role;
			int count = su.update(oa);
			if (count == 0)
				throw new InsertFailedException();
		} catch (NumberFormatException e) {
			log.debug(e.getMessage());
		} 
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Group#addVisualization(net.groupme.model.GroupVisualization,
	 *      net.groupme.model.User)
	 */
	public void addVisualization(GroupVisualization groupVisualization,
			User user) throws InsertFailedException {
		SqlUpdate su = new SqlUpdate(
				ds,
				"INSERT INTO " + TABLE_USERGROUPVISUALIZATION //$NON-NLS-1$
						+ " ("
						+ USERGROUPVISUALIZATION_USER_ID
						+ ", "
						+ USERGROUPVISUALIZATION_GROUP_ID
						+ ", "
						+ USERGROUPVISUALIZATION_VISUALIZATION_ID
						+ ", " 
						+ USERGROUPVISUALIZATION_ACTUAL 
						+ ", "
						+ USERGROUPVISUALIZATION_ROLE + ") VALUES (?, ?, ?, ?, ?)"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		su.declareParameter(new SqlParameter("uID", Types.INTEGER)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("gID", Types.INTEGER)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("vID", Types.INTEGER)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("actual", Types.BOOLEAN)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("role", Types.VARCHAR)); //$NON-NLS-1$
		su.compile();
		try {
			Object[] oa = new Object[5];
			oa[0] = new Integer(user.getId());
			oa[1] = new Integer(this.id);
			oa[2] = new Integer(groupVisualization.getId());
			//TODO: default von actual sollte false sein.
			oa[3] = new Boolean(true);
			oa[4] = DEFAULT_ROLE;
			int count = su.update(oa);
			if (count == 0)
				throw new InsertFailedException();
		} catch (NumberFormatException e) {
			log.debug(e.getMessage());
		} 
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Group#getActualGroupVisualization(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	public GroupVisualization getActualGroupVisualization(String userID) throws GroupVisualizationException {
		query = "SELECT " + USERGROUPVISUALIZATION_VISUALIZATION_ID + " FROM " + TABLE_USERGROUPVISUALIZATION //$NON-NLS-1$ //$NON-NLS-2$
				+ " WHERE " + USERGROUPVISUALIZATION_GROUP_ID + " = " + id + " AND " + USERGROUPVISUALIZATION_USER_ID + " = " + userID
				+ " AND " + USERGROUPVISUALIZATION_ACTUAL + " = " + true; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		GetGroupVisualizationsQuery getVisualizationsQuery = new GetGroupVisualizationsQuery(
				ds, query, USERGROUPVISUALIZATION_VISUALIZATION_ID);
		Iterator iter = getVisualizationsQuery.execute().iterator();
		
		//if you cannot find a visualization for the user then take the visualization of one owner.
		if(!iter.hasNext()){
			for(User owner: this.getCreators()){
				userID = owner.getId();
			}
			query = "SELECT " + USERGROUPVISUALIZATION_VISUALIZATION_ID + " FROM " + TABLE_USERGROUPVISUALIZATION //$NON-NLS-1$ //$NON-NLS-2$
			+ " WHERE " + USERGROUPVISUALIZATION_GROUP_ID + " = " + id + " AND " + USERGROUPVISUALIZATION_USER_ID + " = " + userID
			+ " AND " + USERGROUPVISUALIZATION_ACTUAL + " = " + true; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
			getVisualizationsQuery = new GetGroupVisualizationsQuery(ds, query, USERGROUPVISUALIZATION_VISUALIZATION_ID);
			iter = getVisualizationsQuery.execute().iterator();
		}
		if (iter.hasNext()){
			Collection<GroupVisualization> visualizations = (Collection<GroupVisualization>) iter.next();
			if (visualizations.isEmpty()) throw new GroupVisualizationException("No actual Visualization found for the Group with URI: " + this.getURI());
			else if (visualizations.size() > 1) throw new GroupVisualizationException("Too many actual Visualizations found for the Group with URI: " + this.getURI() + " (has to be exactly #1, but was #" + visualizations.size() + ").");
			else return visualizations.iterator().next();
		}
		else throw new GroupVisualizationException("No actual Visualization found for the Group with URI: " + this.getURI());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Group#getResources()
	 */
	@SuppressWarnings("unchecked")
	public Collection<Resource> getResources() {
		query = "SELECT " + GROUPRESOURCE_RESOURCEID + " FROM " + TABLE_GROUPRESOURCE + " WHERE " + GROUPRESOURCE_GROUPID + " = " + id; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		GetResourcesQuery getResourcesQuery = new GetResourcesQuery(ds, query,
				GROUPRESOURCE_RESOURCEID);
		
		Iterator iter = (getResourcesQuery.execute().iterator());
		if (iter.hasNext()){	
		return (Collection<Resource>) iter.next();
		}
		return new ArrayList<Resource>();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Group#getStandardVisualization()
	 */
	public GroupVisualization getStandardVisualization() {
		// FIXME: Implement this method by selecting randomly a visualization of any user...
		// Wo in der DB gehoert das hin?
		if(getVisualizations().isEmpty()){
			return null;
		}
		return getVisualizations().iterator().next();
	}

	/*
	 * (non-Javadoc)import org.eclipse.jdt.internal.compiler.ast.ThisReference;
	 * 
	 * @see net.groupme.model.Group#getUsers()
	 */
	@SuppressWarnings("unchecked")
	public Collection<User> getUsers() {
		query = "SELECT " + USERGROUP_USER_ID + " FROM " + TABLE_USERGROUP + " WHERE " + USERGROUP_GROUP_ID + " = " + id; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		GetUsersQuery getUsersQuery = new GetUsersQuery(ds, query,
				USERGROUP_USER_ID);
		Iterator iter = getUsersQuery.execute().iterator();
		if (iter.hasNext())
		return (Collection<User>) iter.next();
		return new ArrayList<User>();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Group#getUsers(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	public Collection<User> getUsers(String role) {
		query = "SELECT " + USERGROUP_USER_ID + " FROM " + TABLE_USERGROUP + " WHERE " + USERGROUP_GROUP_ID + " = " + id //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
				+ " AND " + USERGROUP_ROLE + " = \"" + role + "\""; //$NON-NLS-1$ //$NON-NLS-2$
		GetUsersQuery getUsersQuery = new GetUsersQuery(ds, query,
				USERGROUP_USER_ID);
		
		Iterator iter =  getUsersQuery.execute().iterator();
		if (iter.hasNext())
		return (Collection<User>) iter.next();
		return new ArrayList<User>();
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Group#getCreators()
	 */
	public Collection<User> getCreators() {
		Collection<User> creators = new HashSet<User>();
		creators.addAll(getUsers(MODEL_CONSTANTS.ROLE_OWNER));
		creators.addAll(getUsers(MODEL_CONSTANTS.ROLE_MODERATOR));
		return creators;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Group#getVisualization()
	 */
	@SuppressWarnings("unchecked")
	public Collection<GroupVisualization> getVisualizations() {
		query = "SELECT " + USERGROUPVISUALIZATION_VISUALIZATION_ID + " FROM " + TABLE_USERGROUPVISUALIZATION + " WHERE " + USERGROUPVISUALIZATION_GROUP_ID + " = " + id; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		GetGroupVisualizationsQuery getVisualizationsQuery = new GetGroupVisualizationsQuery(
				ds, query, USERGROUPVISUALIZATION_VISUALIZATION_ID);
		Iterator iter = getVisualizationsQuery.execute().iterator();
		if (iter.hasNext()){
			return (Collection<GroupVisualization>) iter.next();
		}
		return new ArrayList<GroupVisualization>();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Group#getVisualization(net.groupme.model.User)
	 */
	@SuppressWarnings("unchecked")
	public Collection<GroupVisualization> getVisualizations(User user) {
		query = "SELECT " + USERGROUPVISUALIZATION_VISUALIZATION_ID + " FROM " + TABLE_USERGROUPVISUALIZATION //$NON-NLS-1$ //$NON-NLS-2$
				+ " WHERE " + USERGROUPVISUALIZATION_GROUP_ID + " = " + id + " AND " + USERGROUPVISUALIZATION_USER_ID + " = " + user.getId(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		GetGroupVisualizationsQuery getVisualizationsQuery = new GetGroupVisualizationsQuery(
				ds, query, USERGROUPVISUALIZATION_VISUALIZATION_ID);
		return (Collection<GroupVisualization>) getVisualizationsQuery
				.execute().iterator().next(); 
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Group#removeResource(net.groupme.model.Resource)
	 */
	public void removeResource(Resource resource) throws DeleteFailedException {
		SqlUpdate su = new SqlUpdate(ds,
				"DELETE FROM " + TABLE_GROUPRESOURCE + " WHERE " //$NON-NLS-1$ //$NON-NLS-2$
						+ GROUPRESOURCE_GROUPID
						+ " = ? AND " + GROUPRESOURCE_RESOURCEID + " = ?"); //$NON-NLS-1$ //$NON-NLS-2$
		su.declareParameter(new SqlParameter("gID", Types.INTEGER)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("rID", Types.INTEGER)); //$NON-NLS-1$
		su.compile();
		try {
			Object[] oa = new Object[2];
			oa[0] = new Integer(this.id);
			oa[1] = new Integer(resource.getId());
			int count = su.update(oa);
			if (count == 0)
				throw new DeleteFailedException();
		} catch (NumberFormatException e) {
			log.debug(e.getMessage());
		} 
		//Method for informing the logging table of the deleting of the resource
		SqlUpdate su2 = new SqlUpdate(ds,
				"Insert Into " + DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION 
				+ " (" + DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_USER_ID + ", "
			    + DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_GROUP_ID + ", "
				+ DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_RESOURCE_ID + ", "
				+ DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_TAG_ID + ", "
				+ DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_ACTION  
				 + ") VALUES (?, ?, ?, ?, ?)");
		su2.declareParameter(new SqlParameter("UserID", Types.INTEGER)); 
		su2.declareParameter(new SqlParameter("GroupID", Types.INTEGER));
		su2.declareParameter(new SqlParameter("ResourceID", Types.INTEGER)); 
		su2.declareParameter(new SqlParameter("TagID", Types.INTEGER)); 		
		su2.declareParameter(new SqlParameter("Action", Types.VARCHAR)); 	
		Object[] oa2 = new Object[5];
		oa2[0] = ((User)(this.getUsers(DATABASE_CONSTANTS.User_ROLE_OWNER).toArray()[0])).getId();
		oa2[1] = this.id;
		oa2[2] = resource.getId();
		oa2[3] = null;
		oa2[4] = DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_RESOURCE_REMOVED;
		su2.update(oa2);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Group#removeUser(net.groupme.model.User)
	 */
	public void removeUser(User user) throws DeleteFailedException {
		SqlUpdate su = new SqlUpdate(ds,
				"DELETE FROM " + TABLE_USERGROUP + " WHERE " //$NON-NLS-1$ //$NON-NLS-2$
						+ USERGROUP_GROUP_ID
						+ " = ? AND " + USERGROUP_USER_ID + " = ?"); //$NON-NLS-1$ //$NON-NLS-2$
		su.declareParameter(new SqlParameter("gID", Types.INTEGER)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("uID", Types.INTEGER)); //$NON-NLS-1$
		su.compile();
		try {
			Object[] oa = new Object[2];
			oa[0] = new Integer(this.id);
			oa[1] = new Integer(user.getId());
			int count = su.update(oa);
			if (count == 0)
				throw new DeleteFailedException();
		} catch (NumberFormatException e) {
			log.debug(e.getMessage());
		} 
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Group#removeVisualization(net.groupme.model.GroupVisualization,
	 *      net.groupme.model.User)
	 */
	public void removeVisualization(GroupVisualization groupVisualization,
			User user) throws DeleteFailedException {
		SqlUpdate su = new SqlUpdate(
				ds,
				"DELETE FROM " + TABLE_USERGROUPVISUALIZATION + " WHERE " + USERGROUPVISUALIZATION_GROUP_ID
						+ " = ? AND " + USERGROUPVISUALIZATION_VISUALIZATION_ID + " = ? AND " + USERGROUPVISUALIZATION_USER_ID + " = ?"); //$NON-NLS-1$ //$NON-NLS-2$
		su.declareParameter(new SqlParameter("gID", Types.INTEGER)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("vID", Types.INTEGER)); //$NON-NLS-1$
		su.declareParameter(new SqlParameter("uID", Types.INTEGER)); //$NON-NLS-1$
		su.compile();
		try {
			Object[] oa = new Object[3];
			oa[0] = new Integer(this.id);
			oa[1] = new Integer(groupVisualization.getId());
			oa[2] = new Integer(user.getId());
			int count = su.update(oa);
			if (count == 0)
				throw new DeleteFailedException();
		} catch (NumberFormatException e) {
			log.debug(e.getMessage());
		} 
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Group#setStandardVisualization(net.groupme.model.GroupVisualization)
	 */
	public void setStandardVisualization(GroupVisualization groupVisualization) {
		// TODO Auto-generated method stub
		// Wo in der DB geh�rt das hin?

	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Identifiable#getURI()
	 */
	public URI getURI() {
		try {
			return new URI(VIEW_VOCABULARY.RESTful_GROUP_URI_PATTERN + id);//$NON-NLS-1$
		} catch (URISyntaxException e) {
			e.printStackTrace();
			return null;
		} 
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.dao.ResourceDAOImpl#getURL()
	 */
	@Override
	public URL getURL() {
		return URLResolver.getGroupURL(this.id);
	}

	
	/*
	 * (non-Javadoc)
	 * 
	 * @see net.groupme.model.Group#getURLOfAPictureInGroup()
	 */
	public String getURLOfAPictureInGroup() {
	String url = null;
	Iterator it = this.getResources().iterator();
	Resource res = null;
	while(it.hasNext()){
		res = (Resource)it.next();
		if (res.getType().equals("image")){
			url = res.getURL().toString();
			break;
		}
	}
	return url;
	}

}
